package com.arcaneshift.model.validators.user;

import com.arcaneshift.model.request.user.LoginRequest;
import com.arcaneshift.security.errors.CredentialsError;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.security.abstracts.AbstractError;

public class LoginValidator extends AbstractValidator {

	public static final int USERNAME_MIN_LENGTH = 6;
	public static final int USERNAME_MAX_LENGTH = 35;
	public static final int PASSWORD_MIN_LENGTH = 10;
	public static final int PASSWORD_MAX_LENGTH = 40;
	
	public boolean validate(AbstractRequest abstractRequest) {
		LoginRequest request = null;
		
		if (abstractRequest.getClass().equals(LoginRequest.class)) {
			request = (LoginRequest) abstractRequest;
		} else {
			error = new CredentialsError(AbstractError.INVALID_REQUEST);
			return false;
		}
		
		String username = request.getUsername();
		String password = request.getPassword();
		
		if (username == null) {
			error = new CredentialsError(CredentialsError.NO_USERNAME_PROVIDED);
			return false;
		}
		
		if (password == null) {
			error = new CredentialsError(CredentialsError.NO_PASSWORD_PROVIDED);
			return false;
		}
		
		if (username.length() < USERNAME_MIN_LENGTH || username.length() > USERNAME_MAX_LENGTH) {
			error = new CredentialsError(CredentialsError.USERNAME_BAD_FORMAT);
			return false;
		}
		
		if (password.length() < PASSWORD_MIN_LENGTH || password.length() > PASSWORD_MAX_LENGTH) {
			error = new CredentialsError(CredentialsError.PASSWORD_BAD_FORMAT);
			return false;
		}
		
		return true;
	}
}
